package sv.gob.mtps.sib.controller;

import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import sv.gob.mtps.sib.model.Objetoespecifico;
import sv.gob.mtps.sib.model.ProductoHasObjetoespecifico;
import sv.gob.mtps.sib.service.ObjetoespService;
import sv.gob.mtps.sib.service.ProductoService;

@Controller
public class ObjetoespController {

	@Autowired
	private ObjetoespService objetoespService;
	
	@Autowired
	private ProductoService productoService;

	@RequestMapping("/admin/errOE")
	public String err(Map<String, Object> map,
			@ModelAttribute("oe") @Valid Objetoespecifico oe,
			BindingResult result) {
		map.put("oe", oe);
		map.put("oeList", objetoespService.listOE());
		return "/admin/showOE";
	}

	@RequestMapping("/admin/showOE")
	public String listOE(Map<String, Object> map) {
		map.put("oe", new Objetoespecifico());
		map.put("oeList", objetoespService.listOE());
		return "/admin/oe";
	}

	@RequestMapping(value = "/admin/addOE", method = RequestMethod.POST)
	public String addOE(
			@ModelAttribute("oe") @Valid Objetoespecifico oe,
			BindingResult result) {
		if (!result.hasErrors()) {

			objetoespService.addOE(oe);
			return "redirect:/admin/showOE";
		} else {
			return "redirect:/admin/errOE";
		}

	}

	@RequestMapping(value = "/admin/editOE", method = RequestMethod.POST)
	public String updateOE(
			@ModelAttribute("oe") @Valid Objetoespecifico oe,
			BindingResult result) {
		if (!result.hasErrors()) {
			objetoespService.updateOE(oe);
			return "redirect:/admin/showOE";
		} else {
			return "redirect:/admin/errOE";
		}

	}

	@RequestMapping(value = "/admin/deleteOE", method = RequestMethod.POST)
	public String deleteOE(
			@ModelAttribute("oe") Objetoespecifico oe,
			BindingResult result) {
			objetoespService.removeOE(oe.getIdObjetoespecifico());
		return "redirect:/admin/showOE";
	}
	
	@RequestMapping(value = "/admin/showPOE", method = RequestMethod.POST)
	public ModelAndView showPOE(@ModelAttribute("oe") Objetoespecifico oe, BindingResult result, ModelMap map) {
		//ModelAndView mav = new ModelAndView("/admin/agregarProducto_OE");
		ProductoHasObjetoespecifico poe = new ProductoHasObjetoespecifico();		
		poe.setObjetoespecifico(oe);
		System.out.println(poe.getObjetoespecifico().getNombreObjetoespecifico());
		map.put("oe", oe);
		map.put("poe", poe);
		map.put("poeList", objetoespService.listPOE());
		map.put("productoList", productoService.listProducto());		
		return new ModelAndView("/admin/agregarProducto_OE",map);
	}
	
	@RequestMapping(value = "/admin/addPOE", method = RequestMethod.POST)
	public ModelAndView addPOE(@ModelAttribute("poe") ProductoHasObjetoespecifico poe, BindingResult result, ModelMap map){
		System.out.println(poe.getObjetoespecifico());
		map.put("poe", poe);
		map.put("poeList", objetoespService.listPOE());
		map.put("productoList", productoService.listProducto());
		objetoespService.addPOE(poe);
		return new ModelAndView("/admin/agregarProducto_OE",map);
	}
	
	@RequestMapping(value = "/admin/editPOE", method = RequestMethod.POST)
	public ModelAndView updatePOE(@ModelAttribute("poe") ProductoHasObjetoespecifico poe, BindingResult result, ModelMap map){
		System.out.println(poe.getObjetoespecifico());
		map.put("poe", poe);
		map.put("poeList", objetoespService.listPOE());
		map.put("productoList", productoService.listProducto());
		objetoespService.updatePOE(poe);
		return new ModelAndView("/admin/agregarProducto_OE",map);
	}
	
	@RequestMapping(value = "/admin/deletePOE", method = RequestMethod.POST)
	public ModelAndView deletePOE(@ModelAttribute("poe") ProductoHasObjetoespecifico poe, BindingResult result, ModelMap map){
		System.out.println(poe.getObjetoespecifico());
		map.put("poe", poe);
		map.put("poeList", objetoespService.listPOE());
		map.put("productoList", productoService.listProducto());
		objetoespService.removePOE(poe.getIdProductoHasObjetoespecifico());
		return new ModelAndView("/admin/agregarProducto_OE",map);
	}
	
}
